//package com.wbk.lotterysystemgateway.filter;
//
//import com.coolgua.match.applet.common.bean.ResponseBean;
//import com.coolgua.match.applet.common.util.ResponseUtil;
//import com.coolgua.match.applet.entity.VodFile;
//import com.coolgua.match.applet.service.FileService;
//import com.coolgua.match.applet.service.VodFileService;
//import com.coolgua.match.applet.service.VodService;
//import com.qcloud.vod.VodUploadClient;
//import com.qcloud.vod.model.VodUploadRequest;
//import com.qcloud.vod.model.VodUploadResponse;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.File;
//import java.util.Date;
//import java.util.UUID;
//
///*******************************************************************************
// * @project: xgyz_new
// * @package: com.coolgua.match.applet.service.impl
// * @author: wbk
// * @created: 2021/3/15
// * @purpose:
// ******************************************************************************/
//@Service
//@Slf4j
//public class VodServiceImpl implements VodService {
//
//    @Value("${match.upload-dir}")
//    private String path;
//
//    @Value("${cloud.tencent.secretId}")
//    private String secretId;
//
//    @Value("${cloud.tencent.secretKey}")
//    private String secretKey;
//
//    @Value("${ffmpeg.location}")
//    private String ffmpeg;
//
//    @Autowired
//    VodFileService vodFileService;
//
//    @Autowired
//    FileService fileService;
//
//    @Override
//    public ResponseBean<VodFile> uploadVideo(MultipartFile video) {
//        VodFile vodFile = null;
//        String videoOriginalFilename = video.getOriginalFilename();
//        String[] s1 = videoOriginalFilename.split("\\.");
//        File videoFile = new File(path + "/" + UUID.randomUUID().toString().replace("-", "") + "." + s1[s1.length - 1]);
//        if (!videoFile.getParentFile().exists()) {
//            videoFile.getParentFile().mkdirs();
//        }
//        VodUploadClient client = new VodUploadClient(secretId, secretKey);
//        VodUploadRequest request = new VodUploadRequest();
//        File videoFile1 = null;
//        File coverFile = null;
//        try {
//            video.transferTo(videoFile);
//            videoFile1 = videoTranscoding(videoFile);
//            log.info("转码后的视频路径:{}", videoFile1.getAbsolutePath());
//            coverFile = generateCover(videoFile);
//            log.info("截取的封面路径:{}", coverFile.getAbsolutePath());
//
//            request.setMediaFilePath(videoFile1.getAbsolutePath());
//            request.setCoverFilePath(coverFile.getAbsolutePath());
//            log.info("uploadVideo>>> MediaFilePath:{}",videoFile1.getAbsolutePath());
//            log.info("uploadVideo>>> coverFilePath:{}",coverFile.getAbsolutePath());
//            VodUploadResponse response = client.upload("ap-guangzhou", request);
//            vodFile = new VodFile(response.getFileId(), response.getMediaUrl(), response.getCoverUrl(), response.getRequestId(), new Date());
//            vodFileService.save(vodFile);
//        } catch (Exception e) {
//            // 业务方进行异常处理
//            log.error("上传失败:", e);
//        }
//        videoFile.delete();
//        videoFile1.delete();
//        coverFile.delete();
//        return ResponseUtil.success(vodFile);
//    }
//
//    private File videoTranscoding(File videoFile) throws Exception {
//        File file = new File(videoFile.getParentFile() + "/" + UUID.randomUUID().toString().replace("-", "") + ".mp4");
//        // /usr/local/ffmpeg/bin/ffmpeg -i 646a37f360c24b65acca9873f8bc6488.mp4  -c:v libx264  ./test.mp4
//        String command = ffmpeg + " -i " + videoFile.getAbsolutePath() + "  -c:v libx264  " + file.getAbsolutePath();
//        log.info("执行ffmpeg对视频转码:{}", command);
//
//        Runtime runtime = Runtime.getRuntime();
//        Process process = runtime.exec(command);
//        process.waitFor();
//        return file;
//    }
//
//    private File generateCover(File videoFile) throws Exception {
//        File file = new File(videoFile.getParentFile() + "/" + UUID.randomUUID().toString().replace("-", "") + ".jpg");
//        // 截取2s处的画面
//        String second = "2";
//        // /usr/local/ffmpeg/bin/ffmpeg -i 646a37f360c24b65acca9873f8bc6488.mp4 -y -f image2 -ss 2 -s 800*600 ./test.jpg
//        String command = ffmpeg + " -i " + videoFile.getAbsolutePath() + " -y -f image2 -ss " + second + " -s 800*600 " + file.getAbsolutePath();
//        log.info("执行ffmpeg截取封面:{}", command);
//
//        Runtime runtime = Runtime.getRuntime();
//        Process process = runtime.exec(command);
//        process.waitFor();
//        return file;
//    }
//}
